<template>
	<view>
		<!-- #ifdef APP-PLUS -->
		<view>
			<uni-nav-bar title="订单" :statusBar='true' :fixed="true"></uni-nav-bar>
		</view>
		<!-- #endif -->
		<view class="order">
			<view class="order-title">
				<view @click="onClickSelectIndex(0)" :class="{ 'order-title-item-active': selectIndex === 0 }" class="order-title-item">今日订单</view>
				<view @click="onClickSelectIndex(1)" :class="{ 'order-title-item-active': selectIndex === 1 }" class="order-title-item">过去一周</view>
				<view @click="onClickSelectIndex(2)" :class="{ 'order-title-item-active': selectIndex === 2 }" class="order-title-item">全部订单</view>
			</view>
			<!-- #ifdef APP-PLUS -->
			<scroll-view @scrolltolower="todayList" :scroll-with-animation='true' scroll-y v-show="selectIndex === 0" :style="{ 'display': selectIndex === 0 ? 'flex' : 'none', 'height': appListHeight + 'px' }" class="order-list">
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<scroll-view @scrolltolower="todayList" :scroll-with-animation='true' scroll-y v-show="selectIndex === 0" :style="{ 'display': selectIndex === 0 ? 'flex' : 'none', 'height': h5ListHeight + 'px' }" class="order-list">
			<!-- #endif -->
				<view v-for="item of orders_today" :key="item.id" class="order-list-item">
					<view class="order-list-item-name">
						<text class="order-list-item-name-left">{{ item.storeName }}</text>
						<text class="order-list-item-name-right">实付：¥{{ item.payAmount }} </text>
					</view>
					<view class="order-list-item-line"></view>
					<view class="order-list-item-info">
						<image class="order-list-item-info-avatar"></image>
						<view class="order-list-item-info-info">
							<text class="order-list-item-info-info-bh">订单编号：{{ item.id }}</text>
							<text class="order-list-item-info-info-sj">下单时间：{{ item.createdAt }}</text>
							<view class="order-list-item-info-info-je">
								<text class="order-list-item-info-info-je-left">小票金额：¥{{ item.price }}</text>
								<text class="order-list-item-info-info-je-right">会员折扣：{{ item.rate }}折</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<!-- #ifdef APP-PLUS -->
			<scroll-view @scrolltolower="weekList" :scroll-with-animation='true' scroll-y v-show="selectIndex === 1" :style="{ 'display': selectIndex === 1 ? 'flex' : 'none', 'height': appListHeight + 'px' }" class="order-list">
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<scroll-view @scrolltolower="weekList" :scroll-with-animation='true' scroll-y v-show="selectIndex === 1" :style="{ 'display': selectIndex === 1 ? 'flex' : 'none', 'height': h5ListHeight + 'px' }" class="order-list">
			<!-- #endif -->
				<view v-for="item of orders_week" :key="item.id" class="order-list-item">
					<view class="order-list-item-name">
						<text class="order-list-item-name-left">{{ item.storeName }}</text>
						<text class="order-list-item-name-right">实付：¥{{ item.payAmount }} </text>
					</view>
					<view class="order-list-item-line"></view>
					<view class="order-list-item-info">
						<image class="order-list-item-info-avatar"></image>
						<view class="order-list-item-info-info">
							<text class="order-list-item-info-info-bh">订单编号：{{ item.id }}</text>
							<text class="order-list-item-info-info-sj">下单时间：{{ item.createdAt }}</text>
							<view class="order-list-item-info-info-je">
								<text class="order-list-item-info-info-je-left">小票金额：¥{{ item.price }}</text>
								<text class="order-list-item-info-info-je-right">会员折扣：{{ item.rate }}折</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
			<!-- #ifdef APP-PLUS -->
			<scroll-view @scrolltolower="allList" :scroll-with-animation='true' scroll-y v-show="selectIndex === 2" :style="{ 'display': selectIndex === 2 ? 'flex' : 'none', 'height': appListHeight + 'px' }" class="order-list">
			<!-- #endif -->
			<!-- #ifndef APP-PLUS -->
			<scroll-view @scrolltolower="allList" :scroll-with-animation='true' scroll-y v-show="selectIndex === 2" :style="{ 'display': selectIndex === 2 ? 'flex' : 'none', 'height': h5ListHeight + 'px' }" class="order-list">
			<!-- #endif -->
				<view v-for="item of orders_all" :key="item.id" class="order-list-item">
					<view class="order-list-item-name">
						<text class="order-list-item-name-left">{{ item.storeName }}</text>
						<text class="order-list-item-name-right">实付：¥{{ item.payAmount }} </text>
					</view>
					<view class="order-list-item-line"></view>
					<view class="order-list-item-info">
						<image class="order-list-item-info-avatar"></image>
						<view class="order-list-item-info-info">
							<text class="order-list-item-info-info-bh">订单编号：{{ item.id }}</text>
							<text class="order-list-item-info-info-sj">下单时间：{{ item.createdAt }}</text>
							<view class="order-list-item-info-info-je">
								<text class="order-list-item-info-info-je-left">小票金额：¥{{ item.price }}</text>
								<text class="order-list-item-info-info-je-right">会员折扣：{{ item.rate }}折</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import { config } from '@/utils/config.js';
	import uniNavBar from '@/components/uni-nav-bar/uni-nav-bar.vue'
	export default {
		data() {
			return {
				appListHeight: 0,
				h5ListHeight: 0,
				selectIndex: 0,
				// 今日
				orders_today: [],
				page_today: 1,
				size_today: 10,
				total_today: 0,
				totalPage_today: 0,
				// 过去一周的
				orders_week: [],
				page_week: 1,
				size_week: 10,
				total_week: 0,
				totalPage_week: 0,
				// 全部订单
				orders_all: [],
				page_all: 1,
				size_all: 10,
				total_all: 0,
				totalPage_all: 0,
			}
		},
		components: {
			uniNavBar
		},
		onLoad() {
			const token = uni.getStorageSync('d_token')
			if (!token) {
				uni.showToast({
					icon: 'none',
					title: '尚未登录, 请先登录'
				})
				setTimeout(() => {
					uni.removeStorageSync('d_role')
					uni.removeStorageSync('d_memberId')
					uni.removeStorageSync('d_token')
					uni.removeStorageSync('d_openId')
					uni.reLaunch({
						url: '../verificationcodelogin/verificationcodelogin'
					})
				}, 777)
				return false
			}
			this._getTodayOrders()
			this._getWeekOrders()
			this._getAllOrders()
		},
		// 下拉刷新
		onPullDownRefresh() {
			if (this.selectIndex === 0) {
				this.orders_today = []
				this.page_today = 1
				this.size_today = 10
				this.total_today = 0
				this.totalPage_today = 0
				this._getTodayOrders()
			} else if (this.selectIndex === 1) {
				this.orders_week = []
				this.page_week = 1
				this.size_week = 10
				this.total_week = 0
				this.totalPage_week = 0
				this._getWeekOrders()
			} else if (this.selectIndex === 2) {
				this.orders_all = []
				this.page_all = 1
				this.size_all = 10
				this.total_all = 0
				this.totalPage_all = 0
				this._getAllOrders()
			}
		},
		onReady() {
			const that = this
			uni.getSystemInfo({
				success: ((res) => {
					let info = uni.createSelectorQuery().select('.order-title');
					info.boundingClientRect(function(data) {
						that.h5ListHeight = res.windowHeight - data.height // 小程序和H5是可以这么设置，但是到了自定义头部的时候，是不允许这么设置的
						that.appListHeight = res.windowHeight - res.statusBarHeight - 44 - data.height
					}).exec()
				})
			})
		},
		methods: {
			todayList() {
				const page1 = this.page_today + 1;
				const totalPage = this.totalPage_today
				if (page1 <= totalPage) {
					this.page_today = page1
					this._getTodayOrders()
				} else {
					uni.showToast({
						title: '已经到底部了',
						icon: 'none'
					})
				}
			},
			weekList() {
				const page1 = this.page_week + 1;
				const totalPage = this.totalPage_week
				if (page1 <= totalPage) {
					this.page_week = page1
					this._getWeekOrders()
				} else {
					uni.showToast({
						title: '已经到底部了',
						icon: 'none'
					})
				}
			},
			allList() {
				const page1 = this.page_all + 1;
				const totalPage = this.totalPage_all
				if (page1 <= totalPage) {
					this.page_all = page1
					this._getAllOrders()
				} else {
					uni.showToast({
						title: '已经到底部了',
						icon: 'none'
					})
				}
			},
			onClickSelectIndex(index) {
				this.selectIndex = index
			},
			pageTotal(rowCount, pageSize) {
				if (rowCount === null || rowCount === "") {
					return 0;
				} else {
					if ((pageSize !== 0) && (rowCount % pageSize === 0)) {
						return parseInt(rowCount / pageSize)
					}
					if ((pageSize !== 0) && (rowCount % pageSize !== 0)) {
						return parseInt(rowCount / pageSize) + 1
					}
				}
			},
			// 今日的
			_getTodayOrders() {
				const page = this.page_today
				const size = this.size_today
				uni.request({
					url: `${config.api_base_url}agent/orders/findTodayOrders/${page}/${size}`,
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('d_token')
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true) {
							this.total_today = data.total
							this.totalPage_today = this.pageTotal(this.total_today, this.size_today)
							this.orders_today = this.orders_today.concat(data.rows)
						} else {
							this.total_today = 0
							this.totalPage_today = 0
							this.orders_today = []
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '查询失败, 网络异常'
						})
					}),
					complete: (() => {
						uni.stopPullDownRefresh()
					})
				})
			},
			// 本周的
			_getWeekOrders() {
				const page = this.page_week
				const size = this.size_week
				uni.request({
					url: `${config.api_base_url}agent/orders/findWeekOrders/${page}/${size}`,
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('d_token')
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true) {
							this.total_week = data.total
							this.totalPage_week = this.pageTotal(this.total_week, this.size_week)
							this.orders_week = this.orders_week.concat(data.rows)
						} else {
							this.total_week = 0
							this.totalPage_week = 0
							this.orders_week = []
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '查询失败, 网络异常'
						})
					}),
					complete: (() => {
						uni.stopPullDownRefresh()
					})
				})
			},
			// 全部的
			_getAllOrders() {
				const page = this.page_all
				const size = this.size_all
				uni.request({
					url: `${config.api_base_url}agent/orders/findTotalOrders/${page}/${size}`,
					header: {
						AUTH: 'ROBOT ' + uni.getStorageSync('d_token')
					},
					success: ((res) => {
						let { status, data } = res.data
						if (status.flag === true) {
							this.total_all = data.total
							this.totalPage_all = this.pageTotal(this.total_all, this.size_all)
							this.orders_all = this.orders_all.concat(data.rows)
						} else {
							this.total_all = 0
							this.totalPage_all = 0
							this.orders_all = []
						}
					}),
					fail: (() => {
						uni.showToast({
							icon: 'none',
							title: '查询失败, 网络异常'
						})
					}),
					complete: (() => {
						uni.stopPullDownRefresh()
					})
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #F7F5F6;
	}
</style>
<style scoped>
	.order {
		width: 100%;
	}

	.order-title {
		width: 100%;
		height: 64rpx;
		background-color: #fff;
		box-sizing: border-box;
		padding-left: 16rpx;
		padding-right: 14rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		border-top: 1px solid #F6F4F5;
	}

	.order-title-item {
		width: 33.33%;
		height: 100%;
		font-size: 28rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-weight: 600;
	}

	.order-title-item-active {
		color: #F02230;
		position: relative;
	}

	.order-title-item-active::after {
		position: absolute;
		left: 50%;
		margin-left: -31rpx;
		bottom: -8rpx;
		content: ' ';
		width: 62rpx;
		height: 4rpx;
		border-radius: 2rpx;
		background-color: #F02230;
	}

	.order-list {
		width: 100%;
		box-sizing: border-box;
		padding-top: 16rpx;
		padding-left: 16rpx;
		padding-right: 14rpx;
	}

	.order-list-item {
		width: 100%;
		background-color: #fff;
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		padding-top: 34rpx;
		padding-left: 40rpx;
		padding-right: 42rpx;
		padding-bottom: 36rpx;
		margin-bottom: 12rpx;
	}

	.order-list-item:last-child {
		margin-bottom: 0;
	}

	.order-list-item-name {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.order-list-item-name-left {
		font-size: 30rpx;
		line-height: 42rpx;
		color: rgba(0, 0, 0, 0.85);
	}

	.order-list-item-name-right {
		font-size: 30rpx;
		line-height: 42rpx;
		color: #999;
		margin-right: 4rpx;
		margin-left: auto;
	}

	.order-list-item-line {
		width: 100%;
		height: 4rpx;
		margin-top: 24rpx;
		background-color: #EEEEEE;
	}

	.order-list-item-info {
		margin-top: 22rpx;
		width: 100%;
		display: flex;
		align-items: flex-start;
	}

	.order-list-item-info-avatar {
		width: 86rpx;
		height: 86rpx;
		border-radius: 4rpx;
		box-sizing: border-box;
		border: 2rpx solid #979797;
		margin-right: 20rpx;
	}

	.order-list-item-info-info {
		display: flex;
		flex-direction: column;
	}

	.order-list-item-info-info-bh {
		font-size: 24rpx;
		color: #999999;
		line-height: 34rpx;
		font-weight: 500;
		margin-bottom: 6rpx;
	}

	.order-list-item-info-info-sj {
		font-size: 24rpx;
		color: #999999;
		line-height: 34rpx;
		font-weight: 500;
		margin-bottom: 6rpx;
	}

	.order-list-item-info-info-je {
		display: flex;
		align-items: center;
	}

	.order-list-item-info-info-je-left {
		font-size: 24rpx;
		font-weight: 500;
		color: #999;
		line-height: 34rpx;
	}

	.order-list-item-info-info-je-right {
		margin-left: 60rpx;
		font-size: 24rpx;
		font-weight: 500;
		color: #999;
		line-height: 34rpx;
	}
</style>
